#include "videodisplaywidget.h"
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QMimeData>
#include <QDebug>
#include <QTime>

VideoDisplayWidget::VideoDisplayWidget(QWidget *parent) : QWidget(parent)
{
    // 创建网格布局
    gridLayout = new QGridLayout(this);
    gridLayout->setContentsMargins(5, 5, 5, 5);
    gridLayout->setSpacing(5);

    // 默认4分屏
    setChannelCount(1);

    // 设置背景色
    setStyleSheet("background-color: #1e1e1e;");

    // 设置拖拽支持`
    setAcceptDrops(true);

}

void VideoDisplayWidget::clearAllWidget()
{
    // 清空所有视频窗口
    for (auto it = videoWidgets.begin(); it != videoWidgets.end(); ++it) {
        gridLayout->removeWidget(it.value());
        delete it.value();
    }
    videoWidgets.clear();
}


VideoDisplayWidget::~VideoDisplayWidget()
{
    clearAllWidget();
}

void VideoDisplayWidget::dragEnterEvent(QDragEnterEvent *event)
{
    event->acceptProposedAction();
}

void VideoDisplayWidget::dropEvent(QDropEvent *event)
{

    qDebug() << "dropEvent";


    QPoint pos = event->pos();

    VideoWidget* targetWidget = findVideoWidgetAt(pos);
    if(targetWidget){
        QString rtmpUrl = event->mimeData()->text();
        targetWidget->resize(targetWidget->size());
        targetWidget->show();
        targetWidget->handleDrag(rtmpUrl);

        if(currentChannelCount == 1)
        {
            qDebug() << "current url : " << videoWidgets[0]->getUrl();

            emit videoWidgets[0]->clicked(videoWidgets[0]->getUrl());
        }

        emit videoWidgets[0]->recordinfo(QTime::currentTime().toString(), QString("播放成功"));
    }





}

void VideoDisplayWidget::setChannelCount(int count)
{
    // 清空现有通道
    clearAllWidget();

    // 根据通道数量设置网格
    int cols;
    switch (count) {
        case 1:
            cols = 1;
            break;
        case 4:
            cols = 2;
            break;
        case 9:
            cols = 3;
            break;
        case 16:
            cols = 4;
            break;
        default:
            cols = 2; // 默认4分屏
            count = 4;
            break;
    }

    // 创建通道标签
    int totalWidgets = cols * cols;
    currentChannelCount = totalWidgets;
    for (int i = 0; i < totalWidgets; ++i) {
        VideoWidget* video = new VideoWidget(this);
        videoWidgets.insert(i, video);

        int row = i / cols;
        int col = i % cols;
        gridLayout->addWidget(video, row, col);

        connect(video, &VideoWidget::clicked, this, &VideoDisplayWidget::videoWidgetClicked);
        connect(video , &VideoWidget::recordinfo , this , &VideoDisplayWidget::transferinfo);
    }

}


VideoWidget* VideoDisplayWidget::findVideoWidgetAt(const QPoint &pos) const
{
    qDebug() << "Finding widget at position:" << pos;
    qDebug() << "Checking" << videoWidgets.size() << "widgets";

    // 遍历所有VideoWidget，检查哪个包含指定位置
    for (auto it = videoWidgets.constBegin(); it != videoWidgets.constEnd(); ++it) {
        VideoWidget* widget = it.value();
        if (widget) {
            QRect widgetRect = widget->geometry();
            qDebug() << "Widget" << it.key() << "geometry:" << widgetRect;
            if (widgetRect.contains(pos)) {
                qDebug() << "Found matching widget:" << it.key();
                return widget;
            }
        }
    }

    // 如果没有找到，尝试使用childAt方法
    QWidget* child = childAt(pos);
    if (child && child != this) {
        qDebug() << "Found child widget:" << child;
        // 检查这个child是否是VideoWidget或者其父级是否是VideoWidget
        QWidget* parent = child;
        while (parent && parent != this) {
            VideoWidget* videoWidget = qobject_cast<VideoWidget*>(parent);
            if (videoWidget) {
                qDebug() << "Found VideoWidget through childAt";
                return videoWidget;
            }
            parent = parent->parentWidget();
        }
    }

    return nullptr;
}



void VideoDisplayWidget::handleDragVideoStream(const QString& rtmpUrl)
{
    // 处理拖拽视频流
    // 这个函数会在MainWindow中被调用，将RTMP URL传递给当前选中的通道


    qDebug() << "handleDragVideoStream";



    /*
    if (!currentChannel.isEmpty()) {
        setChannelVideo(currentChannel, rtmpUrl);
    }
    */
}

